<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@ include file="/WEB-INF/jsp/common/header.jsp" %>

</head>
<body>
	<form>
	<table>
		<tr>
			<td>商品名称</td><td><input type="text" name="name"/></td>
		</tr>
		<tr>
			<td>商品价格</td><td><input type="text" name="price" class="easyui-numberbox"/></td>
		</tr>
		<tr>
			<td>商品毛重</td><td><input type="text" name="weight"/></td>
		</tr>
		<tr>
			<td>商品状态</td><td>下架<input type="radio" name="onsale" value="false" checked/>
								上架<input type="radio" name="onsale" value="true"/>
			</td>
		</tr>
		<tr>
			<td>上架时间</td><td><input type="text" name="onsaleDate" class="easyui-my97"/></td>
		</tr>
		<tr>
			<td>库存</td><td><input type="text" name="store" class="easyui-numberbox"/></td>
		</tr>
		<tr>
			<td>详情页</td><td><input type="text" name="detail"/></td>
		</tr>
		<tr>
			<td>商品类别</td><td><input type="text" name="category_id" id="categoryCombo"/></td>
		</tr>
		
	</table>
	<table>
		<tbody id="attrs"></tbody>
		<tfoot>
			<tr>
				<td colspan="2"><input type="submit" value="提交"/></td>
			</tr>
		</tfoot>
	</table>
	</form>
	<script type="text/javascript">
		function getLevel(id) {
			if(id / 100 % 100 > 0) {
				return 3;
			} else if(id / 10000 % 100 > 0) {
				return 2;
			} else {
				return 1;
			}
		}
		
		$("#categoryCombo").combotree({
			url:"${root}/admin/attr/findAll",
			dataPlain: true,
			textField: "text",
			valueField: "id",
			onSelect: function(record){
				if(getLevel(record.id) != 3){
					$("#categoryCombo").combotree("clear");
					$.messager.alert("只能为3级类别添加商品");
					return;
				}
				$.get("${root}/admin/attr/findByCategoryFetch", {cid:record.id}, function(json){
					console.log(json);
					var $attrs = $("#attrs").empty();
					for(var i = 0; i < json.rows.length; i++) {
						var html = "<tr><td>"+json.rows[i].name+"</td><td><input type='text' class='attrCombo'></td></tr>";
						$(html).appendTo($attrs);
					}
					var combos = $attrs.find(".attrCombo");
					for(var i = 0; i < json.rows.length; i++) {
						$(combos[i]).combobox({
							textField: "name",
							valueField: "id",
							data: json.rows[i].attrValues
						});
					}
					
				});
			}
		});
	
		
	</script>
</body>
</html>